﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Templates
{
    public class Strip : ITemplate, IXmlBuildable, IChildDeletable
    {
        public List<ControlRef> ControlRefs { get; set; }

        public Strip()
        {
            this.ControlRefs = new List<ControlRef>();
        }

        public Strip(XmlNode node):this()
        {
            try
            {
                foreach (XmlNode crefNode in node.SelectNodes("ControlRef"))
                {
                    ControlRef cref = new ControlRef(crefNode);
                    this.ControlRefs.Add(cref);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode rowNode)
        {
            try
            {
                XmlNode stripNode = XmlHelper.AddNode(rowNode, "Strip");

                foreach (ControlRef cRef in this.ControlRefs)
                {
                    cRef.AddNode(stripNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode stripNode = new TreeNode("Strip");
                stripNode.Tag = this;
                stripNode.ContextMenuStrip = this.BuildContextMenu(stripNode);

                foreach (ControlRef cRef in this.ControlRefs)
                {
                    cRef.BuildTreeViewElement(stripNode);
                }

                parentNode.Nodes.Add(stripNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Control Ref");

            ContextMenuStrip cms = new ContextMenuStrip();
            cms.ItemClicked += new ToolStripItemClickedEventHandler(cms_ItemClicked);
            cms.Tag = relatedNode;
            cms.Items.Add(tsmi);

            return cms;
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

                ControlRef cRef = new ControlRef();
                cRef.BuildTreeViewElement(selectedNode);

                this.ControlRefs.Add(cRef);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is ControlRef)
                {
                    this.ControlRefs.Remove((ControlRef)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
